package com.lijinjiang.component;

import com.lijinjiang.constant.Colors;
import com.lijinjiang.mode.PickerMode;
import com.lijinjiang.mode.SelectMode;
import com.lijinjiang.model.DateModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @Description 选择日期面板
 * @Author lijinjiang
 * @Date 2024-12-30 10:52
 */
public class SelectDatePanel extends JComponent {
    private final JDatePanel datePanel;//年度标签
    private final JLabel yearLabel;//年度标签
    private final JLabel monthLabel;//月份标签
    private SelectMode selectMode;//当前选择模式
    private final DateModel<?> dateModel;//日期模型
    private final PickerMode pickerMode;//选择器模式
    private CalendarPanel calendarPanel;

    public SelectDatePanel(JDatePanel datePanel) {
        this.datePanel = datePanel;
        this.dateModel = datePanel.getDateModel();
        this.pickerMode = datePanel.getPickerMode();
        this.yearLabel = datePanel.getYearLabel();
        this.monthLabel = datePanel.getMonthLabel();
        this.setLayout(new BorderLayout());
        this.setOpaque(false);
        if (pickerMode == PickerMode.YEAR) {
            if (selectMode == null) {
                selectMode = SelectMode.YEAR;
            }
            initSelectYearPanel();
        } else if (pickerMode == PickerMode.MONTH) {
            if (selectMode == null) {
                selectMode = SelectMode.MONTH;
            }
            initSelectMonthPanel();
        } else if (pickerMode == PickerMode.DAY) {
            if (selectMode == null) {
                selectMode = SelectMode.DAY;
            }
            initSelectDayPanel();
        } else if (pickerMode == PickerMode.HOUR || pickerMode == PickerMode.MINUTE || pickerMode == PickerMode.MINUTE_10 || pickerMode == PickerMode.MINUTE_15 ||
                pickerMode == PickerMode.MINUTE_30 || pickerMode == PickerMode.SECOND) {
            if (selectMode == null) {
                selectMode = SelectMode.TIME;
            }
            initSelectTimePanel();
        }
        //日期变化监听刷新下年度标签
        dateModel.addChangeListener(e -> {
            refreshTopLabelValue();
        });
    }

    //初始化选择时间面板
    private void initSelectTimePanel() {
        refreshTopLabelValue();
        this.removeAll();
        calendarPanel = new CalendarPanel(dateModel, pickerMode);
        //calendarPanel.addDateSelectionConstraint(new RangeConstraint(LocalDate.of(2024, 1, 1), LocalDate.of(2024, 12, 31)));
        this.add(calendarPanel, BorderLayout.CENTER);
        this.revalidate();//重新布局并绘制
        this.repaint();//重绘
    }

    //初始化选择日期面板
    private void initSelectDayPanel() {
        refreshTopLabelValue();
        this.removeAll();
        calendarPanel = new CalendarPanel(dateModel, pickerMode);
        this.add(calendarPanel, BorderLayout.CENTER);
        this.revalidate();//重新布局并绘制
        this.repaint();//重绘
    }

    //初始化选择年月面板
    public void initSelectMonthPanel() {
        refreshTopLabelValue();
        this.removeAll();
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new GridLayout(4, 3));

        for (int i = 1; i <= 12; i++) {
            String text = i + "月";
            JLabel label = new JLabel(text, JLabel.CENTER);
            label.setOpaque(true);//设置为不透明
            label.setBackground(Colors.BG_YEAR_LABEL);
            //设置选中样式
            if (i == dateModel.getMonth()) {
                label.setForeground(Colors.SELECTED_FONT);
            }
            contentPanel.add(label);
            label.addMouseListener(new MouseAdapter() {
                @Override public void mousePressed(MouseEvent e) {
                    if (e.getButton() == MouseEvent.BUTTON1) {
                        String monthStr = ((JLabel) e.getSource()).getText();
                        int month = Integer.parseInt(monthStr.replace("月", ""));//当前点击的标签上的月份
                        dateModel.setMonth(month);
                        if (pickerMode == PickerMode.MONTH) {
                            dateModel.setSelected(true);//设置选中
                            initSelectMonthPanel();
                        } else if (pickerMode == PickerMode.DAY) {
                            selectMode = SelectMode.DAY;//选择模式改为：日
                            initSelectDayPanel();
                        } else if (pickerMode == PickerMode.HOUR || pickerMode == PickerMode.MINUTE || pickerMode == PickerMode.MINUTE_10 ||
                                pickerMode == PickerMode.MINUTE_15 || pickerMode == PickerMode.MINUTE_30 || pickerMode == PickerMode.SECOND) {
                            selectMode = SelectMode.TIME;//选择模式改为：日
                            initSelectTimePanel();
                        }
                    }
                }
            });
        }
        this.add(contentPanel, BorderLayout.CENTER);
        this.revalidate();//重新布局并绘制
        this.repaint();//重绘
    }

    //初始化选择年面板
    public void initSelectYearPanel() {
        refreshTopLabelValue();
        this.removeAll();
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new GridLayout(4, 3));
        for (int i = 0; i < 12; i++) {
            String text = String.valueOf(dateModel.getTenYear() * 10 + i - 1);
            if (dateModel.getHundredYear() == 0 && dateModel.getTenYear() == 0 && i < 2) {
                text = "";
            }
            JLabel label = new JLabel(text, JLabel.CENTER);
            label.setOpaque(true);//设置为不透明
            label.setBackground(Colors.BG_YEAR_LABEL);
            if (i == 0 || i == 11) {
                label.setForeground(Colors.REDUNDANT_FONT);
            } else if (text.equals(String.valueOf(dateModel.getYear()))) {
                label.setForeground(Colors.SELECTED_FONT);
            }
            contentPanel.add(label);
            label.addMouseListener(new MouseAdapter() {
                @Override public void mousePressed(MouseEvent e) {
                    if (e.getButton() == MouseEvent.BUTTON1) {
                        int year = Integer.parseInt(((JLabel) e.getSource()).getText());//当前点击的标签上的年度
                        dateModel.setYear(year);
                        if (pickerMode == PickerMode.YEAR) {
                            initSelectYearPanel();
                            dateModel.setSelected(true);
                        } else {
                            selectMode = SelectMode.MONTH;//转为月份区间
                            initSelectMonthPanel();
                        }
                    }
                }
            });
        }
        this.add(contentPanel, BorderLayout.CENTER);
        this.revalidate();//重新布局并绘制
        this.repaint();//重绘
    }

    //初始化选择十年面板
    public void initSelectDecadePanel() {
        refreshTopLabelValue();
        this.removeAll();
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new GridLayout(4, 3));
        for (int i = 0; i < 12; i++) {
            int start = dateModel.getHundredYear() * 100 + (i - 1) * 10;
            int end = start + 9;
            String text = start + "-" + end;
            if (dateModel.getHundredYear() == 0 && i == 0) {
                text = "";
            } else if (dateModel.getHundredYear() == 0 && i == 1) {
                text = "1-9";
            }
            JLabel label = new JLabel(text, JLabel.CENTER);
            label.setOpaque(true);//设置为不透明
            label.setBackground(Colors.BG_YEAR_LABEL);
            if (i == 0 || i == 11) {
                label.setForeground(Colors.REDUNDANT_FONT);
            } else if ((start / 10) == dateModel.getYear() / 10) {
                label.setForeground(Colors.SELECTED_FONT);
            }
            contentPanel.add(label);
            label.addMouseListener(new MouseAdapter() {
                @Override public void mousePressed(MouseEvent e) {
                    if (e.getButton() == MouseEvent.BUTTON1) {
                        String yearsSection = ((JLabel) e.getSource()).getText();//十年区间
                        int separatorIndex = yearsSection.indexOf("-");
                        if (separatorIndex > 0) {
                            selectMode = SelectMode.YEAR;//选择模式改为年
                            dateModel.setTenYear(Integer.parseInt(yearsSection.substring(0, separatorIndex)) / 10);
                            initSelectYearPanel();
                        }
                    }
                }
            });
        }
        this.add(contentPanel, BorderLayout.CENTER);
        this.revalidate();//重新布局并绘制
        this.repaint();//重绘
    }

    //点击年标签
    public void clickYearLabel() {
        if (selectMode == SelectMode.MONTH || selectMode == SelectMode.DAY || selectMode == SelectMode.TIME) {
            selectMode = SelectMode.YEAR;
            initSelectYearPanel();
        } else if (selectMode == SelectMode.YEAR) {
            selectMode = SelectMode.DECADE;
            initSelectDecadePanel();
        }
    }

    //点击月标签
    public void clickMonthLabel() {
        if (selectMode == SelectMode.TIME || selectMode == SelectMode.DAY) {
            selectMode = SelectMode.MONTH;
            initSelectMonthPanel();
        }
    }


    //点击超级上一个按钮
    public void clickSuperPreviousButtonAction() {
        switch (selectMode) {
            case TIME:
                if (dateModel.getYear() > 1) {
                    dateModel.addYear(-1);
                    initSelectTimePanel();
                }
                break;
            case DAY:
                if (dateModel.getYear() > 1) {
                    dateModel.addYear(-1);
                    initSelectDayPanel();
                }
                break;
            case MONTH:
                if (dateModel.getYear() > 1) {
                    dateModel.addYear(-1);
                    initSelectMonthPanel();
                    break;
                }
            case YEAR:
                if (dateModel.getTenYear() > 0) {
                    dateModel.addTenYear(-1);
                    initSelectYearPanel();
                }
                break;
            case DECADE:
                if (dateModel.getHundredYear() > 0) {
                    dateModel.addHundredYear(-1);
                    initSelectDecadePanel();
                }
                break;
            default:
                break;
        }
    }

    //点击上一个按钮
    public void clickPreviousButtonAction() {
        switch (selectMode) {
            case TIME:
                if (dateModel.getYear() > 1 || (dateModel.getYear() == 1 && dateModel.getMonth() > 1)) {
                    dateModel.addMonth(-1);
                    initSelectTimePanel();
                }
                break;
            case DAY:
                if (dateModel.getYear() > 1 || (dateModel.getYear() == 1 && dateModel.getMonth() > 1)) {
                    dateModel.addMonth(-1);
                    initSelectDayPanel();
                }
                break;
            default:
                break;
        }
    }

    //点击下一个按钮
    public void clickNextButtonAction() {
        switch (selectMode) {
            case TIME:
                dateModel.addMonth(1);
                initSelectTimePanel();
                break;
            case DAY:
                dateModel.addMonth(1);
                initSelectDayPanel();
                break;
            default:
                break;
        }
    }

    //点击超级下一个按钮
    public void clickSuperNextButtonAction() {
        switch (selectMode) {
            case TIME:
                dateModel.addYear(1);
                initSelectTimePanel();
                break;
            case DAY:
                dateModel.addYear(1);
                initSelectDayPanel();
                break;
            case MONTH:
                dateModel.addYear(1);
                initSelectMonthPanel();
                break;
            case YEAR:
                dateModel.addTenYear(1);
                initSelectYearPanel();
                break;
            case DECADE:
                dateModel.addHundredYear(1);
                initSelectDecadePanel();
                break;
            default:
                break;
        }
    }

    //刷新顶部标签值
    public void refreshTopLabelValue() {
        if (selectMode == SelectMode.DAY || selectMode == SelectMode.TIME) {//日
            datePanel.getPreviousButton().setVisible(true);
            monthLabel.setVisible(true);
            datePanel.getNextButton().setVisible(true);
            String yearLabelValue = dateModel.getYear() + "年";
            String monthLabelValue = dateModel.getMonth() + "月";
            yearLabel.setText(yearLabelValue);
            monthLabel.setText(monthLabelValue);
        } else if (selectMode == SelectMode.MONTH) {//月
            datePanel.getPreviousButton().setVisible(false);
            monthLabel.setVisible(false);
            datePanel.getNextButton().setVisible(false);
            datePanel.getTopCenterPanel().repaint();
            String yearLabelValue = dateModel.getYear() + "年";
            yearLabel.setText(yearLabelValue);
        } else if (selectMode == SelectMode.YEAR) {//年
            datePanel.getPreviousButton().setVisible(false);
            monthLabel.setVisible(false);
            datePanel.getNextButton().setVisible(false);
            if (dateModel.getTenYear() == 0) {
                yearLabel.setText("1-9");
            } else {
                String yearLabelValue = dateModel.getTenYear() * 10 + "-" + (dateModel.getTenYear() * 10 + 9);
                yearLabel.setText(yearLabelValue);
            }
        } else if (selectMode == SelectMode.DECADE) {//十年
            datePanel.getPreviousButton().setVisible(false);
            monthLabel.setVisible(false);
            datePanel.getNextButton().setVisible(false);
            if (dateModel.getHundredYear() == 0) {
                yearLabel.setText("1-99");
            } else {
                String yearLabelValue = dateModel.getHundredYear() * 100 + "-" + (dateModel.getHundredYear() * 100 + 99);
                yearLabel.setText(yearLabelValue);
            }
        }
    }
}
